﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using NK.Mvc3.Models;
using NK.Mvc3.DBL;
using System.Web.Script.Serialization;

namespace NK.Mvc3.Controllers
{ 
    public class SaleAjaxController : Controller
    {
        private StoreContext db = new StoreContext();


        public ActionResult Sale()
        {
            return View();
        }

        public string DeleteDetail(int saleDetailId)
        {
            //TODO
            return "Deleted";
        }

        public ActionResult EditDetail(int id)
        {
            //TODO
            var saleDetail = db.SaleDetails.Find(id);
            return PartialView(saleDetail);
        }

        public ActionResult InsertSaleDetail(int? saleId = null)
        {
            //Sale sale = null;
           
            //if (saleId == null)
            //{
            //    sale = new Sale {DateSale = DateTime.Now};
            //    if (HttpContext.User.Identity.IsAuthenticated)
            //    {
            //        sale.StaffName = HttpContext.User.Identity.Name;
            //    }
            //    db.Sales.Add(sale);
            //    db.SaveChanges();
            //}
            ViewBag.SaleID = saleId;
            return PartialView("InsertSaleDetail2");
        }

        [HttpPost]
        public string InsertSaleDetail(string saleDetail)
        {
            var jsonSerializer = new JavaScriptSerializer();
            var lstSaleDetail = jsonSerializer.Deserialize<List<SaleDetail>>(saleDetail);

            var sale = new Sale
                           {
                               DateSale = DateTime.Now,
                               SaleDetails = new List<SaleDetail>(),
                               StaffName = User.Identity.IsAuthenticated ? User.Identity.Name : string.Empty
                           };

            lstSaleDetail.Remove(null);
            lstSaleDetail.ForEach(delegate(SaleDetail item)
                                      {
                                        item.ProductInfo = db.ProductInfos.Find(item.ProductInfoID);
                                        sale.SaleDetails.Add(item);
                                      });


            db.Sales.Add(sale);
            db.SaveChanges();

            return sale.ID.ToString();
        }

        public ActionResult SaleSummary(int id)
        {
            var sale = db.Sales.Include(s => s.SaleDetails).Where(s => s.ID == id).SingleOrDefault();
            return View(sale);
        }

        [HttpPost]
        public string InsertSaleDetail2(List<SaleDetail> listSales)
        {
            return "InsertSuccess 2";
        }

        //
        // GET: /SaleAjax/

        public ViewResult Index()
        {
            return View(db.Sales.ToList());
        }

        //
        // GET: /SaleAjax/Details/5

        public ViewResult Details(int id)
        {
            Sale sale = db.Sales.Find(id);
            return View(sale);
        }

        //
        // GET: /SaleAjax/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /SaleAjax/Create

        [HttpPost]
        public ActionResult Create(Sale sale)
        {
            if (ModelState.IsValid)
            {
                db.Sales.Add(sale);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(sale);
        }

        [HttpPost]
        public string AjaxCreateSale()
        {
            var sale = new Sale();
            sale.StaffName = User.Identity.IsAuthenticated ? User.Identity.Name : string.Empty;
            sale.DateSale = DateTime.Now;
            db.Sales.Add(sale);
            db.SaveChanges();

            return sale.ID.ToString();
        }
        
        //
        // GET: /SaleAjax/Edit/5
 
        public ActionResult Edit(int id)
        {
            Sale sale = db.Sales.Find(id);
            return View(sale);
        }

        //
        // POST: /SaleAjax/Edit/5

        [HttpPost]
        public ActionResult Edit(Sale sale)
        {
            if (ModelState.IsValid)
            {
                db.Entry(sale).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(sale);
        }

        //
        // GET: /SaleAjax/Delete/5
 
        public ActionResult Delete(int id)
        {
            Sale sale = db.Sales.Find(id);
            return View(sale);
        }

        //
        // POST: /SaleAjax/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Sale sale = db.Sales.Find(id);
            db.Sales.Remove(sale);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}